﻿// ע�

using IotClientService.Models.Lg;
using IotClientService.Models.Simulator;
using IotContract.Dtos.Clients.Equips;
using IotContract.Dtos.Servers.Pushs;
using IotContract.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace IotClientService.Handles.SimulatorHandle;
public abstract class SimulatorHandleBase
{
    public abstract IotEquipType EquipType { get; set; }

    public event EventHandler<SimulatorGeneralDto> SendMsgEvent;
    /// <summary>
    /// 设备
    /// </summary>
    public SimulatorEquipDto Equip { get; set; }

    /// <summary>
    /// 处理信息
    /// </summary>
    /// <param name="msg"></param>
    /// <returns></returns>
    public abstract Task<bool> HandleMsg(SimulatorGeneralDto msg, CommandContent commandContent,string equipType);

    /// <summary>
    /// 触发发送消息
    /// </summary>
    /// <param name="msg"></param>
    public void InvokeSend(SimulatorGeneralDto msg)
    {
        SendMsgEvent?.Invoke(this, msg);
    }

    /// <summary>
    /// 接收数据处理
    /// </summary>
    /// <param name="message"></param>
    /// <param name="lgHkEquipDto"></param>
    /// <returns></returns>
    public async Task HandleData(string message,CommandContent comContent,string equip, SimulatorEquipDto lgHkEquipDto = null)
    {

        try
        {
            SimulatorGeneralDto generalDto = new SimulatorGeneralDto();
            message= message.Trim();
            if (message.Length < 28) return;
            //对传入报文进行解析

            generalDto.Command = message.Substring(0, 4);

            generalDto.EquipCode = message.Substring(4, 8).Trim();

            generalDto.SendTime = message.Substring(12, 14);

            var index = message.IndexOf('$');

            generalDto.Content = message.Substring(index+1);

            HandleMsg(generalDto, comContent,equip);

        }catch (Exception ex)
        {

        }
 

    }


    public string MsgHandle(SimulatorGeneralDto dto)
    {
        return dto.Command + dto.EquipCode + dto.SendTime + dto.ContentHead + dto.Content + dto.ContentEnd;
    }


}
